package com.hemerasolutions.v2trainer.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.event.ActionEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.hemerasolutions.v2trainer.component.interfaces.detail.ModalityDetailsCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.search.ModalitySearchCompInterface;
import com.hemerasolutions.v2trainer.controller.interfaces.ModalityBeanInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.ModalityDetailDelegateInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.ModalitySearchDelegateInterface;
import com.hemerasolutions.v2trainer.model.modality.Modality;
import com.hemerasolutions.v2trainer.model.modality.VersionableModality;
import com.hemerasolutions.v2trainer.model.screen.Reference;
import com.hemerasolutions.v2trainer.service.ExceptionService;
import com.hemerasolutions.v2trainer.service.interfaces.LibraryServiceInterface;
import com.hemerasolutions.v2trainer.service.interfaces.ModalityServiceInterface;
import com.hemerasolutions.v2trainer.util.Utility;

@Controller
@ManagedBean(name="modalityBean")
@Scope("view")
public class ModalityBean extends BaseBean implements ModalityBeanInterface,ModalitySearchDelegateInterface,ModalityDetailDelegateInterface {

	private static final long serialVersionUID = 1L;

	private List<VersionableModality> versionableModalitys;
	
	private VersionableModality versionableModality;
	
	@Autowired
	private ModalityDetailsCompInterface modalityDetailsHandler;
	
	@Autowired
	private ModalityServiceInterface modalityService;
	
	@Autowired
	private LibraryServiceInterface libraryService;	
	
	@Autowired
	private ModalitySearchCompInterface modalitySearch;

	private List<Reference> references;
	
	public ModalityBean() {
		super();
	}

	@PostConstruct
	public void init(){

		this.references = new ArrayList<Reference>();
		this.versionableModalitys = new ArrayList<VersionableModality>();
		this.modalitySearch.setDelegate(this);
		this.modalitySearch.refresh();
		this.modalitySearch.listAll();
		
	}

	
	@Override
	public void onSaveVersionableModality(VersionableModality versionableModality,ActionEvent ev) {
		try{

			boolean saved = false;
			
			for(Modality modality: versionableModality.getModalityVersions()){
				if(this.modalityService.hasChanged(modality)){
					if(!this.modalityService.getReferences(modality).isEmpty()){
						this.references = 	this.modalityService.getReferences(modality);
						
						this.executeJavascript("wcrossReferenceDialog.show();");
					}else{
						modality.setChangedBy(this.getActiveUser());
						modality.setLastChange(Utility.getRoundedDate());

						saved = true;
					}
					
				}
			}

			if(saved){
				this.versionableModality = this.modalityService.saveVersionableModality(versionableModality);
				
				this.toViewVersionableModality();
				
				this.showInfoMessage(	this.localizedText("SavedWithSuccess"),
						this.localizedText("TheModality") + " " +
						this.localizedText("wasSavedWithSuccess"));		

				this.modalitySearch.listAll();

			
			}else{
				this.showErrorMessage(this.localizedText("SaveError"),this.localizedText("NoChanges"));
			}

		} catch (ExceptionService e) {
			this.showErrorMessage(this.localizedText("SaveError"), e.getLocalizedMessage());
			
		}
	}

	@Override
	public void onCreateVersionableModality(VersionableModality versionableModality,ActionEvent ev) {
		try{

			for(Modality modality: versionableModality.getModalityVersions()){

				modality.setChangedBy(this.getActiveUser());
				modality.setCreatedAt(Utility.getRoundedDate());
				modality.setLastChange(Utility.getRoundedDate());
				
			}
				
	
			this.versionableModality = this.modalityService.createVersionableModality(this.versionableModality);
			
			this.toViewVersionableModality();
			
			this.showInfoMessage(	this.localizedText("CreatedWithSuccess"),
					this.localizedText("TheModality") + " " +
					this.localizedText("wasCreatedWithSuccess"));		
				
			
			this.modalitySearch.listAll();

		} catch (ExceptionService e) {
			this.showErrorMessage(this.localizedText("SaveError"), e.getLocalizedMessage());
		}
	}
	

	public void toViewVersionableModality(){
		modalityDetailsHandler.setDelegate(this);
		modalityDetailsHandler.setVersionableModality(this.versionableModality);
		modalityDetailsHandler.setModality(this.versionableModality.getModality());
		modalityDetailsHandler.refresh();
		
	}
	
	public void deleteModality(){
		try{
			modalityService.deleteModality(this.versionableModality.getModality());
			this.showInfoMessage(	this.localizedText("Deleted"),
					this.localizedText("ModalityDeletedWithSuccess"));

			this.versionableModalitys.remove(this.versionableModality);
			this.versionableModality = null;

			this.modalityDetailsHandler.setVersionableModality(this.versionableModality);
			this.modalityDetailsHandler.setModality(null);
			
		}catch(ExceptionService e){
			this.showErrorMessage(this.localizedText("DeleteError"),
					e.getLocalizedMessage());
		}		
	}

	public void deleteVersionableModality(){
		try{
			modalityService.deleteVersionableModality(this.versionableModality);
			this.showInfoMessage(	this.localizedText("Deleted"),
					this.localizedText("ModalityDeletedWithSuccess"));

			this.versionableModalitys.remove(this.versionableModality);
			this.versionableModality = null;

			this.modalityDetailsHandler.setVersionableModality(this.versionableModality);
			this.modalityDetailsHandler.setModality(null);
			
		}catch(ExceptionService e){
			this.showErrorMessage(this.localizedText("DeleteError"),
					e.getLocalizedMessage());
		}
	}
	

	
	public void toAddModality(){

		this.versionableModality = new VersionableModality();	
		
		this.toViewVersionableModality();
		
	}	
	
	
	@Override
	public void onCancelChange(VersionableModality versionableModality) {
		if(this.versionableModality.getId() != 0){
			this.versionableModality = modalityService.readVersionableModality(versionableModality);
		}else{
			this.versionableModality = new VersionableModality();	
		}		

		this.toViewVersionableModality();

	}	

	@Override
	public void onVersionableModalitySearchCompleted(List<VersionableModality> versionableModalitys) {
		this.versionableModalitys.clear();
		this.versionableModalitys.addAll(versionableModalitys);
	}

	@Override
	public boolean onVersionableModalitySelected( VersionableModality versionableModality) {
		return false;
	}

	public List<VersionableModality> getVersionableModalitys() {
		return versionableModalitys;
	}

	public void setVersionableModalitys(
			List<VersionableModality> versionableModalitys) {
		this.versionableModalitys = versionableModalitys;
	}

	public VersionableModality getVersionableModality() {
		return versionableModality;
	}

	public void setVersionableModality(VersionableModality versionableModality) {
		this.versionableModality = versionableModality;
	}

	public ModalityDetailsCompInterface getModalityDetailsHandler() {
		return modalityDetailsHandler;
	}

	public void setModalityDetailsHandler(
			ModalityDetailsCompInterface modalityDetailsHandler) {
		this.modalityDetailsHandler = modalityDetailsHandler;
	}

	public ModalityServiceInterface getModalityService() {
		return modalityService;
	}

	public void setModalityService(ModalityServiceInterface modalityService) {
		this.modalityService = modalityService;
	}

	public LibraryServiceInterface getLibraryService() {
		return libraryService;
	}

	public void setLibraryService(LibraryServiceInterface libraryService) {
		this.libraryService = libraryService;
	}

	public ModalitySearchCompInterface getModalitySearch() {
		return modalitySearch;
	}

	public void setModalitySearch(ModalitySearchCompInterface modalitySearch) {
		this.modalitySearch = modalitySearch;
	}

	@Override
	public void onVersionChanged(Modality modality) {
		// TODO Auto-generated method stub
		
	}

	public List<Reference> getReferences() {
		return references;
	}

	public void setReferences(List<Reference> references) {
		this.references = references;
	}

	
	
}

